package cn.bone.hystrix;

import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixObservableCommand;
import rx.Observable;
import rx.Scheduler;
import rx.Subscriber;
import rx.schedulers.Schedulers;

/**
 * Created by MD-01 on 2017/3/28.
 */
public class HelloHystrixObservableCommand extends
        HystrixObservableCommand<String>{

    private String someBody;
    protected HelloHystrixObservableCommand(String someBody) {
        super(HystrixCommandGroupKey.Factory.asKey("hello"));
        this.someBody = someBody;
    }

    @Override
    protected Observable<String> construct() {

        //生产环境中此处为第三方依赖的具体调用逻辑
        return Observable.create(
                new Observable.OnSubscribe<String>(){
                    @Override
                    public void call(Subscriber<? super String> subscriber) {
                        if(!subscriber.isUnsubscribed()){
                            subscriber.onNext("Hello " + someBody);
                            subscriber.onCompleted();
                        }
                    }
                }).subscribeOn(Schedulers.io());
    }
}
